package com.swpu.cbg.cnshop.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Adapter;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.okhttp.Response;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.adapter.BaseAdapter;
import com.swpu.cbg.cnshop.adapter.CardViewtemDecortion;
import com.swpu.cbg.cnshop.adapter.FavoriteAdatper;
import com.swpu.cbg.cnshop.bean.Favorites;
import com.swpu.cbg.cnshop.bean.Ware;
import com.swpu.cbg.cnshop.http.OkHttpHelper;
import com.swpu.cbg.cnshop.http.SpotsCallback;
import com.swpu.cbg.cnshop.msg.BaseResMsg;
import com.swpu.cbg.cnshop.utils.ToastUtil;
import com.swpu.cbg.cnshop.widget.cnToolbar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MyFavoriteActivity extends AppCompatActivity {

    @ViewInject(R.id.toolbar)
    private cnToolbar mToolbar;


    @ViewInject(R.id.recycler_view)
    private RecyclerView mRecyclerview;

    private FavoriteAdatper mAdapter;

    private OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();
   // private Object favorites;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_favorite);
        ViewUtils.inject(this);
        initToolBar();
        getFavorites();
    }

    private void initToolBar() {
        mToolbar.hideContent();
        mToolbar.showTitle();
        mToolbar.hideEditButton();
        mToolbar.hideRightButton();
    }

    public void getFavorites() {

        Long userId = MyApplication.getInstanse().getUser().getId();

        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", userId);

        okHttpHelper.get(Constans.API.FAVORITE_LIST, params, new SpotsCallback<List<Favorites>>(MyFavoriteActivity.this) {
            @Override
            public void onSuccess(Response response, List<Favorites> favorites) {
              //  showFavorites(favorites);
//                mAdapter.ClearData();
//                mAdapter.AddData(favorites);
                if (mAdapter == null) {
                    showFavorites(favorites);
                } else {
                    mAdapter.refreshData(favorites);
                    mRecyclerview.setAdapter(mAdapter);
                }
               // showFavorites(favorites);
            }

            @Override
            public void onError(Response response, int code, Exception e) {

                LogUtils.d("code:" + code);
            }

            @Override
            public void onTokenError(Response response, int code) {
                System.out.println(code+"");
            }
        });

    }

    private void showFavorites(List<Favorites> favorites) {
        mAdapter = new FavoriteAdatper(this, favorites, R.layout.template_favorite);
        mAdapter.setmListener(new FavoriteAdatper.onItemHandleListener() {
            @Override
            public void removeWare(View view, Ware ware) {
                HashMap<String, Object> params = new HashMap<String, Object>(1);
                params.put("id", ware.getId());
                okHttpHelper.post(Constans.API.FAVORITE_DELETE, params, new SpotsCallback<BaseResMsg>(MyFavoriteActivity.this) {
                    @Override
                    public void onSuccess(Response response, BaseResMsg baseResMsg) {
                        getFavorites();
                        ToastUtil.showToast(MyFavoriteActivity.this, "删除成功");
                    }

                    @Override
                    public void onError(Response response, int code, Exception e) {

                    }

                    @Override
                    public void onTokenError(Response response, int code) {

                    }
                });
            }

            @Override
            public void likeWare(View view, Ware ware) {

            }
        });
        mRecyclerview.setAdapter(mAdapter);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.addItemDecoration(new CardViewtemDecortion());

    }
}
